/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.dialog;

import cn.ac.nercis.pes.community.support.theme.ThemeManager;
import cn.ac.nercis.pes.community.utils.SpringUtils;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

public class PesDialogPane extends DialogPane {
    private final PesDialogLayout dialogLayout;
    private final StackPane dialogRoot;
    private final Button yesButton = new Button();
    private final Button noButton = new Button();
    protected final Boolean showActions;

    protected double xOffset = 0, yOffset = 0;

    @Getter
    private String yesText;
    @Getter
    private String noText;

    public PesDialogPane(){
        this(true);
    }

    public PesDialogPane(boolean showActions){
        this.getStyleClass().add("pes-dialog");
        this.showActions= showActions;
        dialogRoot = new StackPane();
        dialogRoot.getStyleClass().add("pes-dialog-root");
        this.dialogLayout = new PesDialogLayout(showActions);
        this.yesText = "保存";
        this.noText = "关闭";
        if(showActions){
            yesButton.setText(yesText);
            yesButton.getStyleClass().addAll("btn","btn-save");
            noButton.setText(noText);
            noButton.getStyleClass().addAll("btn","btn-back");
            this.dialogLayout.setActions(yesButton,noButton);
        }
        dialogRoot.getChildren().add(this.dialogLayout);
        this.setContent(dialogRoot);
        final List<String> cssList = SpringUtils.findRefCssFiles();
        if(CollectionUtils.isNotEmpty(cssList)){
            cssList.forEach(css -> this.getStylesheets().add(getClass().getResource(css).toExternalForm()));
        }
        this.getStylesheets().add(getClass().getResource(ThemeManager.CURRENT.getCss()).toExternalForm());
        this.dialogLayout.getHeading().setOnMousePressed(this::onMousePressed);
        this.dialogLayout.getHeading().setOnMouseDragged((event)->{
            this.onMouseDragged(event,(Stage)this.getScene().getWindow());
        });
    }

    @Override
    protected Node createButtonBar() {
        return null;
    }

    public StackPane getDialogRoot(){
        return this.dialogRoot;
    }

    public void setYesText(String text){
        this.yesButton.setText(text);
        this.yesText = text;
    }

    public void setNoText(String text){
        this.noButton.setText(text);
        this.noText = text;
    }

    public Button getYesButton(){
        return this.yesButton;
    }

    public Button getNoButton(){
        return this.noButton;
    }

    public void setHeading(Node... titleContent) {
        this.dialogLayout.setHeading(titleContent);
    }

    public void setBody(Node content) {
        this.dialogLayout.setBody(content);
    }

    protected void onMouseDragged(MouseEvent event, Stage stage){
        //根据鼠标的横纵坐标移动dialog位置
        event.consume();
        if (yOffset != 0 ) {
            stage.setX(event.getScreenX() - xOffset);
            if (event.getScreenY() - yOffset < 0) {
                stage.setY(0);
            } else {
                stage.setY(event.getScreenY() - yOffset);
            }
        }

        // 保存窗口改变后的x、y坐标和宽度、高度，用于预判是否会小于最小宽度、最小高度
        double nextX = stage.getX();
        double nextY = stage.getY();
        // 最后统一改变窗口的x、y坐标和宽度、高度，可以防止刷新频繁出现的屏闪情况
        stage.setX(nextX);
        stage.setY(nextY);
    }

    protected void onMousePressed(MouseEvent event){
        event.consume();
        xOffset = event.getSceneX();
        yOffset = event.getSceneY();
    }
}